home *** CD-ROM | disk | FTP | other *** search
- Path: bubba.NMSU.Edu!usenet
- From: ghenniga@ampere.NMSU.Edu (Gary Hennigan)
- Newsgroups: comp.lang.c
- Subject: Re: Ability to locate spaces?
- Date: 21 Feb 1996 10:25:51 -0700
- Organization: New Mexico State University - Electromagnetics Group
- Sender: ghenniga@ampere.NMSU.Edu
- Message-ID: <rhtenro1tgg.fsf@ampere.NMSU.Edu>
- References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>
- <harmon.824449354@pegasus.montclair.edu>
- Reply-To: ghenniga@NMSU.Edu
- NNTP-Posting-Host: ampere.nmsu.edu
- In-reply-to: harmon@pegasus.montclair.edu's message of 16 Feb 1996 01:06:54
- -0500
- X-Newsreader: Gnus v5.1
-
- In an article harmon@pegasus.montclair.edu (Derek Harmon) wrote:
- |>string = fgets (buf, 199, input);
- | ^ BANG! Unless you snuck a string = (char *)malloc( ... somewhere before
- | this point, string remains an uninitialized pointer. It's worth noting that
- | fgets() returns the same string that it reads into buf. There is no reason
- | to keep its return value here, and you could discard it with,
- |: fgets(buf, 200, input); /* You don't use buf as a string, so you are */
- |: /* technically allowed all 200 bytes if you */
- |: /* don't intend to (otherwise buf[199] = '\0' */
- |: /* is a good idea). */
-
- Actually, you'll want to check your man page/manual on fgets()
- behavior. At least on all the Unix implementations I've seen,
- fgets(buf, 200, input) would read 199 characters (or to a newline
- character) and then terminate the string with a NULL character
- automatically. Thus, there is no need to manually terminate anything
- read by fgets() or use one less than the size of the character array
- into which the string would be read in the fgets() argument.
-
- Also, you don't need to allocate "string" above. fgets() returns a
- pointer to where the string resides. In this case string would've been
- assigned as:
- string = &buf[0];
-
- by fgets(), assuming, of course, the read was successful.
-
- In other words...READ the man page/manual!!
-
- Gary
- (ghenniga@NMSU.Edu)
-